package com.zx.service.impl.dj;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.dao.dj.StockBranchMapper;
import com.zx.dao.dj.StockMasterMapper;
import com.zx.model.dj.StockBranch;
import com.zx.model.dj.StockMaster;
import com.zx.service.dj.StockManageService;
import com.zx.utils.DateUtils;

/**
 * 
 * @author zx
 *
 */
@Service
public class StockManageServiceImpl implements StockManageService {

    @Autowired
    private StockMasterMapper stockMasterMapper;
    @Autowired
    private StockBranchMapper stockBranchMapper;

    /**
     * 入库
     */
    @Override
    public void addStockMaster(
            String brand, String model, String color, String size, Integer count, String price,
            String supplier, String arrivalTime) {

        // 判断是否已经入库
        List<StockMaster> list = stockMasterMapper.selStockMastersCondition(0, 1, 0, brand, model, color, size);
        if(list == null || list.size() == 0){
            StockMaster sm = new StockMaster();
            sm.setBrand(brand);
            sm.setModel(model);
            sm.setColor(color);
            sm.setSize(size);
            sm.setTotal(count);
            sm.setCount(count);
            
            if(StringUtils.isNotBlank(price)){
                BigDecimal bd = new BigDecimal(price);
                sm.setPrice(bd);
            }
            sm.setSupplier(supplier);
            if(StringUtils.isNotBlank(arrivalTime)){
                sm.setArrivalTime(DateUtils.stringToDate(arrivalTime, "yyyy-MM-dd"));
            }
            sm.setCreateTime(DateUtils.getCurrentTime());
            sm.setUpdateTime(DateUtils.getCurrentTime());
            
            stockMasterMapper.insert(sm);
        }else{
            Long id = list.get(0).getId();
            
            StockMaster sm = new StockMaster();
            sm.setId(id);
            sm.setTotal(list.get(0).getTotal() + count);
            sm.setCount(list.get(0).getCount() + count);
            if(StringUtils.isNotBlank(price)){
                BigDecimal bd = new BigDecimal(price);
                sm.setPrice(bd);
            }
            sm.setSupplier(supplier);
            if(StringUtils.isNotBlank(arrivalTime)){
                sm.setArrivalTime(DateUtils.stringToDate(arrivalTime, "yyyy-MM-dd"));
            }
            sm.setUpdateTime(DateUtils.getCurrentTime());
            stockMasterMapper.updateByPrimaryKeySelective(sm);
        }
        
    }

    /**
     * 总仓查询
     * @param startIndex
     * @param pageSize
     * @param orderField
     * @param orderType
     * @return
     */
    public List<StockMaster> selStockMasters(Integer pageIndex,Integer pageSize, int orderType){
        int startIndex = (pageIndex - 1)*pageSize;
        List<StockMaster> rst = stockMasterMapper.selStockMasters(startIndex, pageSize, orderType);
        return rst;
    }
    public List<StockMaster> selStockMastersCondition(Integer pageIndex,Integer pageSize, int orderType,String sbrand,String smodel,String scolor,String ssiz){
        int startIndex = (pageIndex - 1)*pageSize;
        List<StockMaster> rst = stockMasterMapper.selStockMastersCondition(startIndex, pageSize, orderType,sbrand,smodel,scolor,ssiz);
        return rst;
    }
    public int selStockMastersCount(String sbrand,String smodel,String scolor,String ssiz){
        
        int rst = stockMasterMapper.selStockMastersCount(sbrand,smodel,scolor,ssiz);
        return rst;
    }
    
    /**
     * 出库
     * @param mid
     * @param outCount
     * @param price
     * @param name
     * @param outTime
     * @return
     */
    public boolean deleteGoods(Long mid, Integer outCount, String price, String name,String outTime){
        
        StockMaster sm = stockMasterMapper.selectByPrimaryKey(mid);
        if(sm == null){
            return false;
        }
        
        // 判断是否已经出库
        StockBranch cond = new StockBranch();
        cond.setMid(mid);
        cond.setName(name);
        List<StockBranch> list = stockBranchMapper.selectBySelective(cond);
        if(list == null || list.size() == 0){
            
            StockBranch rb = new StockBranch();
            rb.setName(name);
            rb.setMid(mid);
            rb.setBrand(sm.getBrand());
            rb.setModel(sm.getModel());
            rb.setColor(sm.getColor());
            rb.setSize(sm.getSize());
            rb.setTotal(outCount);
            rb.setCount(outCount);
            
            if(StringUtils.isNotBlank(price)){
                BigDecimal priceBd = new BigDecimal(price);
                rb.setPrice(priceBd);
            }
            
            if(StringUtils.isNotBlank(outTime)){
                rb.setCreateTime(DateUtils.stringToDate(outTime, DateUtils.DATE_TO_STRING_SHORT_PATTERN));
            }
            rb.setUpdateTime(DateUtils.getCurrentTime());
            stockBranchMapper.insertSelective(rb);
        }else{
            StockBranch rb = new StockBranch();
            rb.setId(list.get(0).getId());
            rb.setTotal(list.get(0).getTotal() + outCount);
            rb.setCount(list.get(0).getCount() + outCount);
            
            if(StringUtils.isNotBlank(price)){
                BigDecimal priceBd = new BigDecimal(price);
                rb.setPrice(priceBd);
            }
            rb.setUpdateTime(DateUtils.getCurrentTime());
            stockBranchMapper.updateByPrimaryKeySelective(rb);
            
        }
        
        // 总仓剩余件数
        int lostCount = sm.getCount() - outCount;
        StockMaster sm2 = new StockMaster();
        sm2.setId(sm.getId());
        sm2.setCount(lostCount);
        stockMasterMapper.updateByPrimaryKeySelective(sm2);
        
        return true;
    }
    
    /**
     * 分仓查询
     * @param startIndex
     * @param pageSize
     * @param orderField
     * @param orderType
     * @return
     */
    public List<StockMaster> selStockBranch(Integer pageIndex,Integer pageSize, int orderType){
        int startIndex = (pageIndex - 1)*pageSize;
        List<StockMaster> rst = stockBranchMapper.selStockBranch(startIndex, pageSize, orderType);
        return rst;
    }
    public List<StockMaster> selStockBranchCondition(
            Integer pageIndex,Integer pageSize, int orderType,String sbrand,String smodel,
            String scolor,String ssiz,String shopname,String remindCount){
        int startIndex = (pageIndex - 1)*pageSize;
        List<StockMaster> rst = stockBranchMapper.selStockBranchCondition(
                startIndex, pageSize, orderType,sbrand,smodel,scolor,ssiz,shopname,remindCount);
        return rst;
    }
    public int selStockBranchCount(String sbrand,String smodel,String scolor,String ssiz,String shopname,String remindCount){
        
        int rst = stockBranchMapper.selStockBranchCount(sbrand,smodel,scolor,ssiz,shopname,remindCount);
        return rst;
    }
}